if __name__ == '__main__':
    import sys
    import os.path
    sys.path.insert(0, os.path.abspath('../../../'))
    sys.path.insert(1, os.path.abspath('../../'))

import argot
from bookstore.dictionary.DictionaryLoader import DictionaryLoader
from bookstore.argotObjects.BookArgot import BookArgot
from bookstore.argotObjects.BookArrayArgot import BookArrayArgot
import CommandBookstore
from bookstore import Bookstore
from bookstore.Book import Book

class LocalBookstore(object):
    """
     The Argot type system must be primed on start up of the application.
     
     Two data dictionary files are loaded.  The common.dictionary contains
     various basic data types such as u8ascii.  The bookstore contains
     our applications book and booklist type descriptions.
     
     After the dictionaries are loaded the marshalling functions are bound
     to the data type.  The Book only binds the TypeWriter.  The reading is
     handled automatically by the TypeReaderAuto marshaller.
     
    """
    def setupArgot(self):
        library = argot.TypeLibrarySingleton.getDefault()

        dictionary_map = argot.TypeMap(library)
        argot.read_dictionary( library, DictionaryLoader.getDictionaryStream("../dictionary/common.dictionary"), dictionary_map )
        dictionary_map2 = argot.TypeMap(library)
        argot.read_dictionary( library, DictionaryLoader.getDictionaryStream("../dictionary/bookstore.dictionary"), dictionary_map2 )
        
        argot.bind_common( library )

        library.bind( BookArgot.TYPENAME, argot.TypeReaderAuto( BookArgot.construct ).read, BookArgot.write )
        library.bind( BookArrayArgot.TYPENAME, BookArrayArgot.read, BookArrayArgot.write )
    
    
    def main(self, args = ()):
        try:
            self.setupArgot()

            bookstore = Bookstore.Bookstore()
            command = CommandBookstore.CommandBookstore( bookstore )
        
            command.getCommand()
        except SystemExit:
            print 'Exiting...'
        except:
            import traceback
            traceback.print_exc()

if __name__ == '__main__':
    localbookstore = LocalBookstore()
    localbookstore.main()
